<?php
/**
 * @name IndexController
 * @author root
 * @desc 默认控制器
 * @see http://www.php.net/manual/en/class.yaf-controller-abstract.php
 */
class IndexController extends Yaf\Controller_Abstract {

    /**
     * 调控制器action之前，先会执行这个方法
     */
    public function init()
    {
        echo 'init';

        $this->getView()->setScriptPath(APPLICATION_PATH.'/template');

    }

    /**
     * 默认动作
     * Yaf支持直接把Yaf_Request_Abstract::getParam()得到的同名参数作为Action的形参
     * 对于如下的例子, 当访问http://yourhost/demo/index/index/index/name/root 的时候, 你就会发现不同
     */
	public function indexAction($name = "Stranger") {
		//1. fetch query
		$get = $this->getRequest()->getQuery("get", "default value");

		//2. fetch model
		$model = new SampleModel();

		//3. assign
		$this->getView()->assign("content", $model->selectSample());
		$this->getView()->assign("name", $name);

		//$this->forward('Index','Index','show');

		//获取application.ini配置对象
		$config = Yaf\Application::app()->getConfig();
        //print_r($config);
        //echo $config->application->abc;

        //1 自动加载类库
        //echo Tool\Http::getHost(); //全局类库
        echo '<br/>';
        //echo Admin\Http::getHost();//本地类库
        print_r(User\User::getUser());

        //2.手动加载类库
        //yaf\Loader::import('/usr/local/nginx/html/yaf/demo/libs/Tool/1.php');//文件不存在不会报错，返回false
        var_dump(yaf\Loader::import('Data/category.php'));//先去本地library 目录下查找
        //print_r(Category::$data);

        //yaf.lowease_path  小写路径加载规则
        //ini_set('yaf.lowcase_path',1);
        //echo Tool\Http::getHost();  //Tool  的  T 会变成小写 t

		//4. render by Yaf, 如果这里返回FALSE, Yaf将不会调用自动视图引擎Render模板
        //return TRUE;
        return False;
	}

	public function showAction()
    {
        echo 'show';
        $this->getView()->assign('title','IndexController');
        $this->getView()->assign('username','skinglzw');
        //$msgTpl = $this->getView()->render('index/show.phtml');

        //消息接口............
        //echo $msgTpl;

        $this->getView()->display('index/show.phtml');
        return false;
    }


    public function httpAction()
    {
        //get post file params(yaf 自带的) routes.user.map 通过params这个参数带到控制器的  (params 就是路由时传给控制器的参数)
        $get = $this->getRequest()->getQuery();
        $post = $this->getRequest()->getPost();
        $file = $this->getRequest()->getFiles();
        //$params = $this->getRequest()->getParams();
        echo $this->getRequest()->get('title');
        echo "<br/>";
        var_dump($get);
        var_dump($post);
        var_dump($file);
        //var_dump($params);
        return false;
    }

    public function responseAction()
    {
        //$this->getResponse()->setRedirect('https://www.baidu.com/');  //301 重定向
        $this->getResponse()->setBody('test');
        $this->getResponse()->response();//有两次test了

        echo 1111111111111;

        return false;
        //exit; //第二次test 不会输出了 (代码没执行完，sample.php -> dispatchLoopShutdown没执行到)
        //可以禁用自动响应  application.ini 里设置
    }

}
